package org.fuys.coder.domain.feed.service.impl;

import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.feed.service.IFeedPushService;
import org.fuys.coder.domain.mailbox.model.req.MailReq;
import org.fuys.coder.domain.mailbox.model.vo.MailVO;
import org.fuys.coder.domain.mailbox.service.IMailBoxService;
import org.fuys.coder.domain.user.model.vo.operation.UserFollowTypeVO;
import org.fuys.coder.domain.user.service.repo.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.feed.service.impl
 * @className: FeedPushServiceImpl
 * @author: WangJing
 * @description: feed流推实现类
 * @date: 2024/7/25 23:19
 * @version: 1.0
 */

@Service
public class FeedPushServiceImpl implements IFeedPushService {

    @Resource
    private IUserService userService;

    @Resource
    private IMailBoxService mailBoxService;


    @Override
    public void pushToUser(MailReq mailVO) {
        if(ObjectUtils.isEmpty(mailVO.getToIds())){
            return;
        }
        mailBoxService.batchPublishMail(mailVO.getToIds(),mailVO.getMailVO());
    }

    @Override
    public void pushToFollowers(MailReq mailReq) {
        final MailVO mailVO = mailReq.getMailVO();
        final Integer fromId = mailVO.getFromId();
        if(ObjectUtils.isEmpty(fromId)){
            throw new BusinessException("推送者未知");
        }
        mailVO.setFromId(fromId);
        String nickName=userService.getUserNameById(fromId);
        if(ObjectUtils.isEmpty(nickName)){
            throw new BusinessException(ResultMessageConstants.NO_USER_INFO);
        }
        final List<Integer> userFollowers = userService.getUserFollowers(fromId, UserFollowTypeVO.SPECIAL);
        if(ObjectUtils.isEmpty(userFollowers)){
            return;
        }
        mailVO.setSenderName(nickName);
        mailReq.setToIds(userFollowers);
        mailBoxService.batchPublishMail(mailReq.getToIds(),mailReq.getMailVO());
    }
}
